home *** CD-ROM | disk | FTP | other *** search
/ Best of Shareware / Best of PC Windows Shareware 1.0 - Wayzata Technology (7111) (1993).iso / mac / ZIPPED / DOS / PROGRAMG / GRAD.ZIP / PAPER.EPS < prev    next >
Text File  |  1990-12-30  |  12KB  |  430 lines

  1. 
  2.  
  3.  
  4.  
  5.  
  6.           A SYSTEM FOR THE AUTOMATIC DIFFERENTIATION
  7.                      OF FORTRAN PROGRAMS
  8.  
  9.  
  10.                          Oscar Garcia                                   ' 
  11.                   Forest Research Institute
  12.                      Rotorua, New Zealand
  13.  
  14.  
  15.  
  16.                            ABSTRACT
  17.  
  18.           A general-purpose automatic
  19.           differentiation system is described.
  20.           Given a Fortran subprogram for computing a
  21.           function, it generates a subprogram that
  22.           computes partial derivatives.  The APL
  23.           computer language was used in the
  24.           implementation.
  25.  
  26.  
  27.                          INTRODUCTION
  28.  
  29. In an effort to speed-up the estimation of parameters in
  30. complex forest growth models, an automatic differentiation
  31. system was developed (Garcia 1989, 1991).  It takes as input a                          '                                   
  32. Fortran program unit (usually a subroutine or function
  33. subprogram) that computes the value of a function, and
  34. produces as output another Fortran program unit that computes
  35. partial derivatives with respect to specified independent
  36. variables.
  37.  
  38. I describe here the methods and implementation, and give some
  39. figures on the performance of the generated code.  Additional
  40. discussion and evaluation of results can be found in Garcia                                                         ' 
  41. (1991).
  42.  
  43. Performance appears satisfactory, and compares favorably with
  44. that of JAKEF (Hillstrom 1990).  The system may be useful in
  45. other applications.
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52. ____________________________
  53.  
  54. Presented at the SIAM Workshop on Automatic Differentiation of
  55. Algorithms, Breckenridge, Colorado, January 6-8, 1991.
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.                               2 
  65. 
  66. 
  67.                 PRINCIPLES AND IMPLEMENTATION
  68.  
  69. The basic idea was inspired by Wengert (1964).  He suggested
  70. substituting for each operator a routine, that in addition to
  71. compute the result of the operation would also produce the
  72. values of the derivatives.  A somewhat similar approach was
  73. implemented by hand in order to compute first and second
  74. partial derivatives in a parameter estimation program (Garcia                                                           ' 
  75. 1983).  Each statement in the function evaluation routine was
  76. followed by statements to compute the derivatives of the left-
  77. hand-side variable (usually an intermediate variable).  For
  78. example,
  79.  
  80.         C = A * B
  81. 
  82. would be followed by
  83.  
  84.         C1 = A1 * B + A * B1
  85.         C2 = A2 * B + A * B2
  86.         etc.,
  87. 
  88. where the Ai and Bi are partial derivatives with respect to
  89. the i-th independent variable, computed in previous statements
  90. (with obvious simplifications where the partials for some
  91. terms do not exist).  The function derivatives sought follow
  92. the statement defining the final value of the function,
  93. usually at the end of the subroutine.  Unlike Wengert's, this
  94. method produces stand-alone code, without the overhead of
  95. calls to routines in a run-time package.
  96.  
  97. The system described here essentially automates this
  98. procedure.  The statements that compute derivatives precede
  99. instead of follow each function evaluation statement, to
  100. handle correctly statements such as
  101.  
  102.         A = A * B .
  103. 
  104. For each assignment statement, the right-hand-side expression
  105. is processed by a recursive descent parser, and Fortran code
  106. for computing the relevant derivatives is generated.
  107. Unnecessary parenthesis are avoided as much as possible.
  108. Thus, most of the redundant operations generated will be
  109. removed by any optimizing compiler.  An earlier version
  110. produced fully parenthesized expressions that would inhibit
  111. optimization, since in Fortran parenthesis fix the order of
  112. operations.
  113.  
  114. Identifiers for the derivatives are formed by appending to the
  115. variable name an optional user-defined delimiter character and
  116. the number of the independent variable.  Identifiers exceeding
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.                               3 
  126. 
  127. 
  128. the 6-character Fortran standard maximum length might need to
  129. be changed by the user (a warning is produced).  Many Fortran
  130. compilers, however, have the option of accepting long variable
  131. names (e.g. using the /4Nt switch in Microsoft Fortran).
  132.  
  133. The existence of non-zero derivatives of left-hand-side
  134. temporary variables with respect to the independent variables
  135. is recorded in an internal data structure as the statements
  136. are processed.  Initially, new statements are generated only
  137. for non-zero derivatives.  In some instances this would not
  138. produce correct results, and multiple passes may be needed to
  139. initialize at zero some of the derivatives.  For example, with
  140. one pass the following statements would generate incorrect
  141. code (X is the first independent variable and this is the
  142. first appearance of A; derivatives of VAR with respect to the
  143. k-th independent variable are identified as VAR_k):
  144.  
  145.       IF (I .LT. 5) GO TO 10           IF (I .LT. 5) GO TO 10
  146.         A = 0.                           A = 0.
  147.         GO TO 20                         GO TO 20
  148.    10 CONTINUE                      10 CONTINUE
  149.         A = X               ══>          A_1 = 1.
  150.    20 CONTINUE                           A = X
  151.         B = A + 1                   20 CONTINUE
  152.                                          B_1 = A_1
  153.                                          B = A + 1
  154. 
  155. Here A_1 = 0.0 would be needed following the IF statement.
  156. Similar problems may arise from loops:
  157.  
  158.         A = 0.
  159.         B = 0.
  160.         DO 10 I = 1,N
  161.           A = A + B
  162.           B = C(I) * X
  163.      10 CONTINUE
  164. 
  165. These cases are handled by making multiple passes over the
  166. original code.  The need for another pass is signaled if a new
  167. non-zero derivative is encountered for a variable that had
  168. been previously used on the left-hand-side of an assignment.
  169. Keeping the accumulated record of non-zero derivatives causes
  170. that new derivative to be initialized at zero in the
  171. appropriate place in the next pass.
  172.  
  173. A result of the myopic strategy of examining only assignment
  174. statements, one at a time, was a relatively simple and
  175. efficient system.  However, a few restrictions must be
  176. observed in the source coding:
  177.  
  178.  
  179.  
  180.  
  181.  
  182.  
  183.  
  184.  
  185.  
  186.                               4 
  187. 
  188. 
  189. a) The independent variables must be scalars or array elements
  190. with constant subscripts.
  191.  
  192. b) Assignments involving independent variables directly or
  193. indirectly are not allowed in IF
  194.    or other control statements.
  195.  
  196. c) Labels are not allowed in assignment statements.  Use
  197. CONTINUE.
  198.  
  199. d) User defined functions involving the independent variables
  200. are not supported.
  201.  
  202. In addition, the header of the output routine must be edited
  203. manually to change its name and to include the derivatives as
  204. arguments.  It may also be necessary to declare the new
  205. variables if default types or IMPLICIT declarations are not
  206. used.  The procedures have been used only with Fortran 66, and
  207. designed with this dialect in mind.  It is conceivable that
  208. some additional restrictions could be needed with Fortran 77.
  209.  
  210. Second or higher derivatives can be generated simply by
  211. running the output through the differentiator again.
  212. Different delimiter characters for the identifiers must be
  213. used in each run.  The resulting code, however, will perform
  214. redundant computations, since (for second derivatives) two
  215. versions of the gradient and of the off-diagonal Hessian
  216. elements are generated.  It would not be difficult to
  217. eliminate this redundancy in a post-processing step, but this
  218. has not been implemented yet.
  219.  
  220. The system was written in APL, using STSC's APL*PLUS
  221. interpreter for IBM PC or compatible microcomputers (STSC
  222. 1989).  This is transparent to the user, however, and no
  223. knowledge of APL is required to use it.  The high level and
  224. power of the APL computer language made possible to complete
  225. the implementation in a very short time.
  226.  
  227. The system also runs without modification with the inexpensive
  228. Pocket APL (Turner 1985), and with other APL interpreters from
  229. STSC available for several microcomputers and mainframes.  In
  230. order to make it more accessible, a version for the public
  231. domain I-APL interpreter has been prepared.  Although all
  232. comments were removed to make it fit into the less than 30 K
  233. bytes of memory available under I-APL, this version cannot
  234. process very large problems, and is about 27 times slower than
  235. the APL*PLUS version.
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.                               5 
  248. 
  249. 
  250.                          PERFORMANCE
  251.  
  252. The run-time performance of the generated code may be assessed
  253. by the work ratio (Griewank 1988), the ratio of the time
  254. required to compute a gradient to the time taken by a function
  255. evaluation.  Note that the computation of the gradient
  256. produces also a function value, which in many applications is
  257. also needed.  A forward differences approximation of the
  258. gradient for a function with n independent variables requires
  259. n + 1 function evaluations, that is, a work ratio of n + 1.
  260. The work ratio for central differences is 2n, or 2n + 1 if a
  261. function evaluation at the central point is included.
  262.  
  263. The following table  shows work ratios for several examples.
  264. Work ratios for another automatic differentiator, JAKEF
  265. (Hillstrom 1990), are also included.  The tests were carried
  266. out on AT-compatible microcomputers with floating point
  267. coprocessors.
  268.  
  269.  
  270.      Problem         Variables       Work ratio      JAKEF
  271.         1                3              3.1           11.2
  272.         2                4              2.7           11.1
  273.         3                9              4.0             -
  274.         4               16              5.6             -
  275.         5               18              5.2            8.0
  276.         6               18              6.4             -
  277. 
  278.  
  279. Problems 1 and 2 are two short subroutines used for testing
  280. optimization algorithms.  Problems 3 to 6 are large
  281. subroutines used for parameter estimation, with about 130 to
  282. 180 Fortran statements, and containing loops running over
  283. large numbers of observations.  JAKEF uses a reverse
  284. differentiation approach (Griewank 1988), with storage
  285. requirements increasing with the number of statements
  286. executed.  Only problem 5, where the number of observations
  287. was limited to 100, could be solved with JAKEF in the
  288. available memory (Garcia 1991).                      '        
  289.  
  290.  
  291.  
  292.  
  293.                           REFERENCES
  294.  
  295. Garcia, O. 1983.  A stochastic differential equation model for    '                                                         
  296.     the height growth of forest stands. EBiometrics 39F, 1059-
  297.     1072.
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.                               6 
  309. 
  310. 
  311. Garcia, O. 1989.  Growth Modelling - New developments.  In    '                                                     
  312.     Nagumo, H. and Konohira, Y. (Ed.)  Japan and New Zealand
  313.     Symposium on Forestry Management Planning.  Japan
  314.     Association for Forestry Statistics.
  315.  
  316. Garcia, O. 1991.  Experience with automatic differentiation in    '                                                         
  317.     estimating forest growth model parameters.  Presented at
  318.     the SIAM Workshop on Automatic Differentiation of
  319.     Algorithms, Breckenridge, Colorado, January 6-8, 1991.
  320.  
  321. Griewank, A. 1988.  On automatic differentiation.  Argonne
  322.     National Laboratory, Mathematics and Computer Science
  323.     Division.  Preprint ANL/MCS-P10-1088.
  324.  
  325. Hilstrom, K. E. 1990.  User guide for JAKEF.  (Available in
  326.     electronic form from netlib on Internet).
  327.  
  328. STSC 1989.  APL*PLUS system for the PC - User's manual.  STSC,
  329.     Inc.
  330.  
  331. Turner, J. R. 1985.  Pocket APL - Reference guide.  STSC,
  332.     Inc., Rockville, Maryland.
  333.  
  334. Wengert, R.E. 1964. A simple automatic derivative evaluation
  335.     program. ECommunications of the ACM 7F, 463-471.
  336.  
  337.  
  338.  
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.  
  346.  
  347.  
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.                               7 
  368. 
  369. 
  370.                            APPENDIX
  371.  
  372.                 FORWARD AND REVERSE DIFFERENTIATION
  373.  
  374.                             (Omitted)
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393.  
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.  
  405.  
  406.  
  407.  
  408.  
  409.  
  410.  
  411.  
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419.  
  420.  
  421.  
  422.  
  423.  
  424.  
  425.  
  426.  
  427.  
  428.  
  429.